#include "registerwin.h"
#include "ui_registerwin.h"
#include <QFileDialog>
#include <faceobject.h>
#include <QDebug>
#include <opencv2/opencv.hpp>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QMessageBox>

RegisterWin::RegisterWin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterWin)
{
    ui->setupUi(this);
}

RegisterWin::~RegisterWin()
{
    delete ui;
}

void RegisterWin::timerEvent(QTimerEvent *e)
{
    Q_UNUSED(e)
    if(cap.isOpened())
    {
        cap>>image;
        if(image.data == nullptr)return;
    }

    cv::Mat rgbImage;
    cv::cvtColor(image,rgbImage,cv::COLOR_BGR2RGB);
    QImage img(rgbImage.data,rgbImage.cols,rgbImage.rows,rgbImage.step,QImage::Format_RGB888);

    QPixmap mmp = QPixmap::fromImage(img);
    mmp = mmp.scaledToWidth(ui->headPicLb->width());
    mmp = mmp.scaledToHeight(ui->headPicLb->height());
    ui->headPicLb->setPixmap(mmp);
}

void RegisterWin::on_resetBtn_clicked()
{
    //清空数据
    ui->nameEdit->clear();
    ui->addressEdit->clear();
    ui->phoneEdit->clear();
    ui->picFileEdit->clear();
    ui->birthEdit->setDate(QDate::currentDate());
}

void RegisterWin::on_addpicBtn_clicked()
{
    //通过文件对话框选择图片路径
    QString picPath = QFileDialog::getOpenFileName(this,"open File","C:/Users/32086/Pictures","(*.png *.jpg)");
    ui->picFileEdit->setText(picPath);

    //显示图片
    QPixmap mmp(picPath);
    mmp = mmp.scaledToWidth(ui->headPicLb->width());
    ui->headPicLb->setPixmap(mmp);
}

void RegisterWin::on_addBtn_clicked()
{
    //1.通过照片，结合faceObject模块得到faceID
    FaceObject  faceobj;
    cv::Mat image = cv::imread(ui->picFileEdit->text().toUtf8().data());
    int faceID = faceobj.face_register(image);

    //把头像保存到一个固定的路径下
    QString headfile = QString("./data/%1.jpg").arg(QString(ui->nameEdit->text().toUtf8().toBase64()));
    cv::imwrite(headfile.toUtf8().data(),image);

    //2.把个人信息存储到数据库employee
    QSqlTableModel model;
    model.setTable("employee");
    QSqlRecord record = model.record();

    //设置数据
    record.setValue("name",ui->nameEdit->text());
    record.setValue("sex",ui->Rbtn_man->isChecked() ? "男":"女");
    record.setValue("birthday",ui->birthEdit->text());
    record.setValue("address",ui->addressEdit->text());
    record.setValue("phone",ui->phoneEdit->text());
    record.setValue("faceID",faceID);

    //头像路径
    record.setValue("headfile",headfile);

    //把记录插入到数据库表格中
    bool ret = model.insertRecord(0,record);
    //3.提示个人注册成功
    if(ret)
    {
        QMessageBox::information(this,"注册提示","注册成功");
        //提交
        model.submitAll();
    }
    else
    {
        QMessageBox::information(this,"注册提示","注册失败");
    }
}

void RegisterWin::on_openVideoBtn_clicked()
{
    if(ui->openVideoBtn->text() == "打开摄像头")
    {
        cap.open(0);
        if(!cap.isOpened())
        {
            qDebug()<<"打开失败";
            return;
        }

        ui->openVideoBtn->setText("关闭摄像头");
        timerID = startTimer(100);  //打开定时器
    }
    else
    {
        killTimer(timerID);         //关闭定时器
        ui->openVideoBtn->setText("打开摄像头");
        ui->headPicLb->clear();
        cap.release();
    }
}

void RegisterWin::on_cameraBtn_clicked()
{
    // 构建文件路径
    QString headfile = QString("./data/%1.jpg").arg(QString(ui->nameEdit->text().toUtf8().toBase64()));
    ui->picFileEdit->setText(headfile);

    //处理图片类型
    cv::imwrite(headfile.toUtf8().data(),image);
    killTimer(timerID);//关闭定时器事件
    ui->openVideoBtn->setText("打开摄像头");
    //关闭摄像头
    cap.release();
}
